Write-Host "=== 通过API插入测试数据 ===" -ForegroundColor Green

Write-Host "`n1. 首先尝试登录获取Token..." -ForegroundColor Yellow
try {
    $loginData = @{
        username = "admin"
        password = "password"
    } | ConvertTo-Json
    
    $loginResponse = Invoke-RestMethod -Uri "http://localhost:5252/api/auth/login" -Method POST -Body $loginData -ContentType "application/json"
    
    if ($loginResponse.token) {
        Write-Host "✓ 登录成功，获取到Token" -ForegroundColor Green
        $token = $loginResponse.token
        
        $headers = @{
            "Authorization" = "Bearer $token"
            "Content-Type" = "application/json"
        }
        
        Write-Host "`n2. 检查现有数据..." -ForegroundColor Yellow
        
        # 检查角色数据
        try {
            $roleResponse = Invoke-RestMethod -Uri "http://localhost:5252/api/role/list" -Method GET -Headers $headers
            Write-Host "角色数据: $($roleResponse.Count) 条记录" -ForegroundColor Cyan
        } catch {
            Write-Host "角色API调用失败: $($_.Exception.Message)" -ForegroundColor Red
        }
        
        # 检查权限数据
        try {
            $permissionResponse = Invoke-RestMethod -Uri "http://localhost:5252/api/permission/list" -Method GET -Headers $headers
            Write-Host "权限数据: $($permissionResponse.Count) 条记录" -ForegroundColor Cyan
        } catch {
            Write-Host "权限API调用失败: $($_.Exception.Message)" -ForegroundColor Red
        }
        
        # 检查文章数据
        try {
            $articleResponse = Invoke-RestMethod -Uri "http://localhost:5252/api/article/list" -Method GET -Headers $headers
            Write-Host "文章数据: $($articleResponse.Count) 条记录" -ForegroundColor Cyan
        } catch {
            Write-Host "文章API调用失败: $($_.Exception.Message)" -ForegroundColor Red
        }
        
        Write-Host "`n3. 如果数据为空，尝试创建测试数据..." -ForegroundColor Yellow
        
        # 创建测试角色
        try {
            $roleData = @{
                name = "测试角色"
                description = "这是一个测试角色"
            } | ConvertTo-Json
            
            $createRoleResponse = Invoke-RestMethod -Uri "http://localhost:5252/api/role/create" -Method POST -Body $roleData -Headers $headers
            Write-Host "✓ 创建角色成功" -ForegroundColor Green
        } catch {
            Write-Host "✗ 创建角色失败: $($_.Exception.Message)" -ForegroundColor Red
        }
        
        # 创建测试权限
        try {
            $permissionData = @{
                code = "test_permission"
                name = "测试权限"
                description = "这是一个测试权限"
            } | ConvertTo-Json
            
            $createPermissionResponse = Invoke-RestMethod -Uri "http://localhost:5252/api/permission/create" -Method POST -Body $permissionData -Headers $headers
            Write-Host "✓ 创建权限成功" -ForegroundColor Green
        } catch {
            Write-Host "✗ 创建权限失败: $($_.Exception.Message)" -ForegroundColor Red
        }
        
        # 创建测试文章
        try {
            $articleData = @{
                title = "测试文章"
                content = "这是一个测试文章的内容"
                status = 1
            } | ConvertTo-Json
            
            $createArticleResponse = Invoke-RestMethod -Uri "http://localhost:5252/api/article/create" -Method POST -Body $articleData -Headers $headers
            Write-Host "✓ 创建文章成功" -ForegroundColor Green
        } catch {
            Write-Host "✗ 创建文章失败: $($_.Exception.Message)" -ForegroundColor Red
        }
        
        Write-Host "`n4. 再次检查数据..." -ForegroundColor Yellow
        
        # 重新检查角色数据
        try {
            $roleResponse = Invoke-RestMethod -Uri "http://localhost:5252/api/role/list" -Method GET -Headers $headers
            Write-Host "角色数据: $($roleResponse.Count) 条记录" -ForegroundColor Cyan
        } catch {
            Write-Host "角色API调用失败: $($_.Exception.Message)" -ForegroundColor Red
        }
        
        # 重新检查权限数据
        try {
            $permissionResponse = Invoke-RestMethod -Uri "http://localhost:5252/api/permission/list" -Method GET -Headers $headers
            Write-Host "权限数据: $($permissionResponse.Count) 条记录" -ForegroundColor Cyan
        } catch {
            Write-Host "权限API调用失败: $($_.Exception.Message)" -ForegroundColor Red
        }
        
        # 重新检查文章数据
        try {
            $articleResponse = Invoke-RestMethod -Uri "http://localhost:5252/api/article/list" -Method GET -Headers $headers
            Write-Host "文章数据: $($articleResponse.Count) 条记录" -ForegroundColor Cyan
        } catch {
            Write-Host "文章API调用失败: $($_.Exception.Message)" -ForegroundColor Red
        }
        
    } else {
        Write-Host "✗ 登录失败，未获取到Token" -ForegroundColor Red
    }
} catch {
    Write-Host "✗ 登录失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "`n可能的原因:" -ForegroundColor Yellow
    Write-Host "1. 数据库中没有用户数据" -ForegroundColor White
    Write-Host "2. 后端服务未运行" -ForegroundColor White
    Write-Host "3. 数据库连接问题" -ForegroundColor White
}

Write-Host "`n=== 测试数据插入完成 ===" -ForegroundColor Green 